package com.wangjiang.mario.admin.web.admin.controller.v1;

import com.wangjiang.mario.admin.common.base.BaseController;
import com.wangjiang.mario.admin.common.object.UUidList;
import com.wangjiang.mario.admin.common.object.UUidListForBatchUpdate;
import com.wangjiang.mario.admin.common.rest.ResultGenerator;
import com.wangjiang.mario.admin.common.rest.entity.Result;
import com.wangjiang.mario.admin.system.domain.entity.UserEntity;
import com.wangjiang.mario.admin.system.domain.service.UserService;
import com.wangjiang.mario.admin.system.object.user.UserQo;
import com.wangjiang.mario.admin.system.object.user.UserRegisterDto;
import com.wangjiang.mario.admin.system.object.user.UserVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wangjiang
 */
@RestController
@RequestMapping("/user")
@Api(value = "/user", tags = "用户相关接口")
public class UserController extends BaseController<UserEntity> {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @GetMapping("")
    @RequiresPermissions(value = "system:user:list")
    public Result list (
            UserQo qo,
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return ResultGenerator.genSuccessResult(userService.relatedPage(currentPage, pageSize, qo, UserVo.class));
    }

    @PostMapping("")
    public Result create (@RequestBody UserEntity entity) {
        logger.info("entry is forbidden: {}", entity);
        return ResultGenerator.genForbidenResult("接口禁止访问");
    }

    /**
     * 即将被废弃
     * @param entity
     * @return
     */
    @Deprecated
    @PutMapping("")
    public Result update (@RequestBody UserEntity entity) {
        return ResultGenerator.genSuccessResult(userService.update(entity));
    }

    @PutMapping("/v2")
    public Result updateV2 (@RequestBody UserRegisterDto userRegisterDto) {
        return ResultGenerator.genSuccessResult(userService.updateRegisterUser(userRegisterDto));
    }

    /**
     * 根据uuid 获取实体信息
     *
     * @param uuid
     * @return
     */
    @GetMapping("/{uuid}")
    public Result getOne (@PathVariable String uuid) {
        return ResultGenerator.genSuccessResult(baseService.baseGetByUUid(uuid, UserVo.class));
    }

    @GetMapping("/all")
    public Result all (UserEntity qo) throws IllegalAccessException {
        return ResultGenerator.genSuccessResult(baseService.list(qo));
    }

    @PutMapping("/batch")
    public Result batchUpdate(
            @RequestBody UUidListForBatchUpdate<UserEntity> uuidListForBatchUpdate
    ) {
        return ResultGenerator.genSuccessResult(baseService.baseUpdateBatch(uuidListForBatchUpdate));
    }

    @DeleteMapping("")
    public Result delete(
            @RequestParam String id
    ) {
        return ResultGenerator.genSuccessResult(baseService.baseSoftRemoveById(id));
    }

    @DeleteMapping("/batch")
    public Result batchDelete(
            @RequestBody UUidList uuidList
    ) {
        return ResultGenerator.genSuccessResult(baseService.baseBatchSoftRemoveByUUidList(uuidList));
    }
}
